package com.wyq.action;

import java.io.PrintWriter;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;

import javax.servlet.http.HttpServletResponse;

import org.apache.struts2.ServletActionContext;

import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
import com.wyq.bean.Audience;
import com.wyq.bean.AudienceQInfo;
import com.wyq.bean.Audience_Qtitle;
import com.wyq.service.AudienceQService;
import com.wyq.service.AudienceService;
import com.wyq.service.ShowInfoService;
import com.wyq.util.StringUtil;

public class PreAudienceAction extends ActionSupport {
	private AudienceQService audienceQService;
	private AudienceService audienceService;
	private ShowInfoService showInfoService;
	
	private String type;//用户-audience or 公司-company
	private String source;//展会渠道
	private String pageName;//跳转页面
	
	private Map showInfo;//展会信息
	//验证用户是否存在
	private String name;
	private String value;
	private List audienceList;
	
	//跳转到网站首页
	public String toIndexPage(){
		try{
			Map session = ActionContext.getContext().getSession();
			List<Map> showInfos = showInfoService.getShowInfoByTypeOrUrlChannel(null, source);
			if(null != showInfos && showInfos.size()>0){
				showInfo = showInfos.get(0);
				//session.put("_showInfo",showInfo);
			}
			if(StringUtil.isEmpty(pageName)){
				pageName = "success";
			}
			return pageName;
		}catch(Exception e){
			e.printStackTrace();
		}
		return "fail";
	}
	
	
	//验证用户是否存在 TODO:返回JSon串验证
	public String validateAudienceIsExist(){
		try{
			ActionContext context = ActionContext.getContext();   
			HttpServletResponse response = (HttpServletResponse) context.get(ServletActionContext.HTTP_RESPONSE);
			response.setContentType("text/html;charset=utf-8");
			PrintWriter out=response.getWriter();
			
			//展会ID
			String show_id = getShowInfoId();
			
			List<Audience> audiencelist = audienceService.findAudienceIsRegister(name, value,show_id);
			audienceList = audiencelist;
			
			return "validateSuccess";
		}catch(Exception e){
			e.printStackTrace();
		}
		return "validateSuccess";
	}
	
	//修改用户信息
	public String updateAudienceInfo() throws Exception {
		try{
			ActionContext context = ActionContext.getContext();   
//		HttpServletResponse response = (HttpServletResponse) context.get(ServletActionContext.HTTP_RESPONSE);
//		response.setContentType("text/xml;charset=UTF-8");
//		HttpServletRequest request1 = (HttpServletRequest) context.get(ServletActionContext.HTTP_REQUEST);
//		response.setContentType("text/xml;charset=UTF-8");
			Map request = (Map)ActionContext.getContext().get("request");
			
			//展会ID
			String show_id = getShowInfoId();
			
			//问卷调查的信息
			List QtitleList = audienceQService.findAllAudience_Qtitle(show_id);
			Set qset = ((Audience_Qtitle)QtitleList.get(0)).getAudienceQs();
			System.out.println(QtitleList);
			
			
			//用户登记的信息(从session中得到用户信息)
			Map session = ActionContext.getContext().getSession();
			String audience_name = session.get("audience_name").toString();
			String audience_email = session.get("audience_email").toString();
			String audience_mobile = session.get("audience_mobile").toString();
			//System.out.println("----" + audience_email + "---------" + audience_tel);
			Audience audience = new Audience();
			audience.setAudience_name(audience_name);
			audience.setAudience_Email(audience_email);
			audience.setAudience_mobile(audience_mobile);
			List audienceInfoList = audienceService.getAudienceRegisterInfo(audience);
			Audience audienceInfo = (Audience) audienceInfoList.get(0);
			Set set = audienceInfo.getAudienceQInfos();
			Iterator iterator = set.iterator();
			AudienceQInfo audienceQInfo = null;
			while(iterator.hasNext()){
				audienceQInfo = (AudienceQInfo)iterator.next();
			}
			System.out.println("audienceInfo :" + audienceQInfo.getQInfo());
			//request.put("", value);
			request.put("qTitleList", QtitleList);
			request.put("audienceInfo", audienceInfo);
			request.put("audienceQInfo", audienceQInfo.getQInfo());
			
			return "update_sucess";
			
		}catch(Exception e){
			e.printStackTrace();
			return "update_fail";
		}
	}
	
	//获取展会信息
	private String getShowInfoId() throws Exception{
		//获取展会信息
		String show_id = "";
		List<Map> showInfos = showInfoService.getShowInfoByTypeOrUrlChannel(null, source);
		if(null != showInfos && showInfos.size()>0){
			Map domain = (Map)showInfos.get(0);
			show_id = domain.get("id")!= null?domain.get("id").toString():"";
		}
		return show_id;
	}

	public void setType(String type) {
		this.type = type;
	}
	
	public void setAudienceQService(AudienceQService audienceQService) {
		this.audienceQService = audienceQService;
	}

	public void setAudienceService(AudienceService audienceService) {
		this.audienceService = audienceService;
	}

	public Map getShowInfo() {
		return showInfo;
	}

	public void setShowInfoService(ShowInfoService showInfoService) {
		this.showInfoService = showInfoService;
	}

	public List getAudienceList() {
		return audienceList;
	}

	public void setAudienceList(List audienceList) {
		this.audienceList = audienceList;
	}

	public void setName(String name) {
		this.name = name;
	}

	public void setValue(String value) {
		this.value = value;
	}

	public void setSource(String source) {
		this.source = source;
	}

	public void setPageName(String pageName) {
		this.pageName = pageName;
	}

	public String getSource() {
		return source;
	}
	
	
}
